<?php
/**
 * Copyright 2009, OpenScriptures.org
 * GPL 3 License: http://www.gnu.org/licenses/gpl.html
 * $Id: svn.lib.php 158 2009-02-25 17:25:04Z WestonRuter@gmail.com $
 */



/**
 * Hyperlinks an SVN ID including a link to the source code and to the contributor's home page
 */
function format_svn_id($idstr, $urlstr){
	$url = preg_replace('/\s+\$$/',
						'',
						preg_replace('{^.+?https://open-scriptures.googlecode.com/svn/}',
									 'http://code.google.com/p/open-scriptures/source/browse/',
									 $urlstr));
	$basename = basename($url);
	$id = str_replace($basename,
					 "<a href='$url' title='Source code for this page'>$basename</a>",
					 $idstr);
	$contributorURLs = array(
		'WestonRuter@gmail.com' => 'http://weston.ruter.net/'
	);
	foreach($contributorURLs as $username => $url){
		$id = str_replace($username, "<a title=\"Member's website\" href='$url'>$username</a>", $id);
	}
	print preg_replace('/@\S+?(?=<|\s)/', '', $id);
}



/**
 * This function parses a file for the SVN rev number in the Id and Rev keywords.
 * By including this rev number in the query string of a file (file.ext?rev=6) then
 * far-future expires may be set on those resources.
 * Note: it is better just to get filemtime()
 */
function get_svn_rev_from_file($filename){
	$rev = -1;
	$f = fopen($filename, 'r');
	while (!feof($f)) {
		$contents = fread($f, 512);
		if(preg_match('/\$Id:\s+\S+\s+(\d+)/', $contents, $matches)){
			$rev = $matches[1];
			break;
		}
		else if(preg_match('/\$Rev:\s+(\d+)/', $contents, $matches)){
			$rev = $matches[1];
			break;
		}
	}
	fclose($f);
	return $rev;
}
